package com.liliudong.rommath.biz.sys.entity;

import com.liliudong.rommath.biz.sys.param.SysUserAddParam;
import com.liliudong.rommath.common.base.IdBaseEntity;
import com.liliudong.rommath.common.security.CurrentUser;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.EnumValue;
import com.mybatisflex.annotation.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
 * 系统用户 实体类。
 *
 * @author liudong.li
 * @since 1.0.0
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Table(value = "sys_user")
public class SysUser extends IdBaseEntity {

    /**
     * 账号
     */
    @Column(value = "account")
    private String account;

    /**
     * 密码
     */
    @Column(value = "password")
    private String password;

    /**
     * 状态 0.锁定 1.正常
     */
    @Column(value = "status")
    private Status status;

    /**
     * 头像
     */
    @Column(value = "avatar")
    private String avatar;

    /**
     * 昵称
     */
    @Column(value = "nickname")
    private String nickname;

    /**
     * 部门编号
     */
    @Column(value = "dept_code")
    private String deptCode;

    public static SysUser of(SysUserAddParam param) {
        SysUser sysUser = new SysUser();
        sysUser.setAccount(param.getAccount());
        sysUser.setPassword(param.getPassword());
        sysUser.setStatus(param.getStatus());
        sysUser.setDeptCode(param.getDeptCode());
        return sysUser;
    }

    @Getter
    @RequiredArgsConstructor
    public enum Status {
        /**
         * 锁定
         */
        LOCKED(0),
        /**
         * 正常
         */
        NORMAL(1);

        @EnumValue
        private final int value;
    }

    public CurrentUser toCurrentUser() {
        CurrentUser currentUser = new CurrentUser();
        currentUser.setId(this.id);
        currentUser.setAccount(this.account);
        currentUser.setDeptCode(this.deptCode);
        currentUser.setCreatedBy(this.createdBy);
        currentUser.setCreatedFrom(this.createdFrom);
        return currentUser;
    }

}
